#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# ======================================================
# @File:      : test15
# @Author     : forward_huan
# @Date       : 2024/11/16 19:05
# @Desc       :
# ======================================================
from typing import List


class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        if len(nums) == 3:
            return [nums] if sum(nums) == 0 else []
        result = []
        nums.sort()
        for m in range(len(nums) - 2):
            if m > 0 and nums[m] == nums[m - 1]:
                continue
            first = nums[m]
            right = len(nums) - 1
            for n in range(m + 1, len(nums) - 1):
                if n > (m + 1) and nums[n] == nums[n - 1]:
                    continue
                second = nums[n]
                third = - first - second
                while n < right:
                    if third == nums[right]:
                        result.append([first, second, nums[right]])
                        break
                    elif nums[right] > third:
                        right -= 1
                    else:
                        break
        return result


# print(Solution().threeSum(nums=[-1, 0, 1, 2, -1, -4]))
print(Solution().threeSum(nums=[0, 0, 0, 0]))
